package org.example.gui.widget;

import org.example.gui.listener.MonthPickerListener;
import org.example.gui.panel.WorkingPanel;
import org.example.util.DateUtil;

import javax.swing.*;
import java.awt.*;
import java.util.Date;

/**
 * 外部通过调用 HistoryPickerPanel.instance.date 来获取选择器的时间
 * 本类写死了起始年份并保证了一定的年份使用的扩展性
 */

public class HistoryPickerPanel extends WorkingPanel {
    public static HistoryPickerPanel instance = new HistoryPickerPanel();
    //写死起始年份
    private final int startYear = 2020;
    //当前面板实例的时间
    public Date date = DateUtil.monthBegin();
    public JComboBox<Integer> cbMonth = new JComboBox<>(makeMonths());
    public JComboBox<Integer> cbYear = new JComboBox<>(makeYears());
    private final JButton bSubmit = new JButton("查询");

    private HistoryPickerPanel() {
        this.setLayout(new GridLayout(1, 3, 8, 8));
        //调整到当前月
        cbYear.setSelectedIndex(DateUtil.thisYear() - startYear);
        cbMonth.setSelectedIndex(DateUtil.thisMonth());
        this.add(cbYear);
        this.add(cbMonth);
        this.add(bSubmit);
        addListener();
    }

    /**
     * @return 2020 - 今年的Integer数组
     */
    private Integer[] makeYears() {
        int thisYear = DateUtil.thisYear();
        Integer[] result = new Integer[thisYear - startYear + 1];
        for (int i = 0; i <= thisYear - startYear; i++) {
            result[i] = startYear + i;
        }
        return result;
    }

    /**
     * @return 1-12的Integer数组
     */
    private Integer[] makeMonths() {
        Integer[] result = new Integer[12];
        for (int i = 0; i < 12; i++) {
            result[i] = i + 1;
        }
        return result;
    }

    @Override
    public void updateData() {

    }

    @Override
    public void addListener() {
        bSubmit.addActionListener(new MonthPickerListener());
    }
}
